package com.ztjava.core;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.ztjava.core.log.Log;
import com.ztjava.core.util.FileUtil;

public class InitConfig {
	private final static Log log = Log.getLog(InitConfig.class);
	
	public static void loadPluginConfig(String pluginPackage,String pluginClass){
		log.info("---------"+pluginClass+"配置文件开始加载"+"---------");
		loadAllConfig(pluginPackage.replace(".",File.separator)+File.separator+"config.json");
	}
	public static void loadAllConfig(String path){
		JSONObject config = JSONObject.parseObject(FileUtil.readFileToString(path));
		if(config==null) return;
		loadParam(config);
		loadBaseData(config);
		loadImport(config);
		loadExport(config);
		loadMenu(config);
	}
	
	//加载配置参数
	public static void setParam(String name,String code,String value,String remark){
		Record record = new Record();
		record.set("name", name);
		record.set("code", code);
		record.set("value",value);
		record.set("remark", remark);
		CacheKit.put(Constant.CacheParam, record.getStr("code"), record);
		System.out.println("系统参数："+record.getStr("code")+"="+record.getStr("value"));
	}
	public static void loadParam(JSONObject config){
		JSONArray param = config.getJSONArray("param");
		if(param==null) return;
		JSONObject vo = null;
		for(int i=0;i<param.size();i++){
			vo = param.getJSONObject(i);
			setParam(vo.getString("name"), vo.getString("code"), vo.getString("value"), vo.getString("remark"));
		}
	}
	
	public static void loadBaseData(JSONObject config){
		JSONArray basedate = config.getJSONArray("basedata");
		if(basedate==null) return;
		
		JSONObject code = null;
		JSONArray items = null;
		JSONObject vo = null;
		for(int i=0;i<basedate.size();i++){
			code = basedate.getJSONObject(i);
			Map<String,Record> map = new LinkedHashMap<String,Record>();
			items = code.getJSONArray("items");
			for(int j=0;j<items.size();j++){
				vo = items.getJSONObject(j);
				Record record = new Record();
				record.set("name", vo.getString("name"));
				record.set("value", vo.getString("value"));
				if(StrKit.notNull(vo.get("color"))){
					record.set("color", vo.getString("color"));
				}
				map.put(vo.getString("value"), record);
			}
			CacheKit.put(Constant.CacheData, code.getString("code"), map);
			System.out.println("数据字典："+code.getString("code"));
		}
	}
	
	public static void loadImport(JSONObject config){
		JSONArray importJson = config.getJSONArray("import");
		if(importJson==null) return;
		JSONObject item = null;
		JSONArray columns = null;
		JSONObject column = null;
		for(int i=0;i<importJson.size();i++){
			item = importJson.getJSONObject(i);
			columns = item.getJSONArray("columns");
			Record importData = new Record();
			List<Record> result = new ArrayList<Record>();
			for(int j=0;j<columns.size();j++){
				column = columns.getJSONObject(j);
				Record record = new Record();
				record.set("name", column.getString("name"));
				record.set("field", column.getString("field"));
				if(column.get("repeat")!=null){
					record.set("repeat", column.getBoolean("repeat"));
				}else {
					record.set("repeat", true);
				}
				if(column.get("must_have")!=null){
					record.set("must_have", column.getBoolean("must_have"));
				}else {
					record.set("must_have", true);
				}
				if(column.get("blank")!=null){
					record.set("blank", column.getBoolean("blank"));
				}else {
					record.set("blank", true);
				}
				if(column.get("change")!=null){
					record.set("change", column.getString("change"));
				}
				if(column.get("date")!=null){
					record.set("date",column.getString("date"));
				}
				if(column.get("max")!=null){
					record.set("max", column.getInteger("max"));
				}
				if(column.get("min")!=null){
					record.set("min", column.getInteger("min"));
				}
				result.add(record);
			}
			importData.set("columns", result);
			importData.set("tablename", item.getString("tablename"));
			if(item.get("perSize")!=null){
				importData.set("perSize", item.getInteger("perSize"));
			}else {
				importData.set("perSize", 10);
			}
			CacheKit.put(Constant.CacheData,item.getString("code"), importData);
			System.out.println("数据导入配置："+item.getString("code"));
		}
	}

	public static void loadExport(JSONObject config){
		JSONArray exportJson = config.getJSONArray("export");
		if(exportJson==null) return;
		JSONObject item = null;
		JSONArray columns = null;
		JSONObject column = null;
		for(int i=0;i<exportJson.size();i++){
			item = exportJson.getJSONObject(i);
			columns = item.getJSONArray("columns");
			List<Record> result = new ArrayList<Record>();
			for(int j=0;j<columns.size();j++){
				column = columns.getJSONObject(j);
				Record record = new Record();
				record.set("name", column.getString("name"));
				record.set("field", column.getString("field"));
				if(column.get("change")!=null){
					record.set("change", column.getString("change"));
				}
				result.add(record);
			}
			CacheKit.put(Constant.CacheData,item.getString("code"), result);
			System.out.println("数据导出配置："+item.getString("code"));
		}
	}

	//读取菜单表
	public static void loadMenu(JSONObject config){
		JSONArray menu = config.getJSONArray("menu");
		if(menu==null) return;
		JSONObject typeJSON = null;
		JSONArray menusJSON = null;
		for(int a=0;a<menu.size();a++){
			typeJSON = menu.getJSONObject(a);
			menusJSON = typeJSON.getJSONArray("menu");
			List<Record> menuList = CacheKit.get(Constant.CacheData,"CONFIG_"+typeJSON.getString("type"));
			if(menuList==null){
				menuList = new ArrayList<Record>();
			}
			menuList.addAll(menuJSONToObject(menusJSON,0));
			CacheKit.put(Constant.CacheData,"CONFIG_"+typeJSON.getString("type"), menuList);
		}
	}
	public static List<Record> menuJSONToObject(JSONArray jsonArray,Integer level){
		level = level + 1;
		JSONObject json = null;
		List<Record> menuList = new ArrayList<Record>();
		for(int i=0;i<jsonArray.size();i++){
			json = jsonArray.getJSONObject(i);
			Record record = new Record();
			record.set("level", level);
			record.set("name", json.getString("name"));
			record.set("rights", json.getString("rights"));
			if(json.get("url")!=null){
				record.set("url", json.getString("url"));
			}
			if(json.get("icon")!=null){
				record.set("icon", json.getString("icon"));
			}else {
				if(json.get("children")!=null){
					record.set("icon", "fa fa-clone");
				}else {
					record.set("icon", "fa fa-genderless");
				}
			}
			if(json.get("children")!=null){
				record.set("children", menuJSONToObject(json.getJSONArray("children"), level));
			}
			menuList.add(record);
		}
		return menuList;
	}
}
